﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for setting.xaml
    /// </summary>
    public partial class setting : Window
    {
        public string s;
        private bool isFirst;
        public setting()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            fromDatePicker.SelectedDate = DateTime.Now;
            toDatePicker.SelectedDate = DateTime.Now;
            fromDatePicker.Visibility = System.Windows.Visibility.Hidden;
            FromLabel.Visibility = System.Windows.Visibility.Hidden;
            toDatePicker.Visibility = System.Windows.Visibility.Hidden;
            toLabel.Visibility = System.Windows.Visibility.Hidden;
            comboBox1.Visibility = System.Windows.Visibility.Hidden;
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (attributFilterBox.SelectedIndex < 0)
            {

            }
            else
            {
                s = nameFilterBox.Text;
                homeWindow.top.isFilter = true;
                if (homeWindow.top.tabel == topWindow.REKLAME)
                {
                    if (attributFilterBox.SelectedItem.ToString() == "Kota")
                    {
                        homeWindow.top.filter[0] = true;
                        homeWindow.top.filter[1] = false;
                    }
                    else
                    {
                        homeWindow.top.filter[0] = false;
                        homeWindow.top.filter[1] = true;
                    }
                }
                else if (homeWindow.top.tabel == topWindow.PENYEWAAN)
                {
                    if (attributFilterBox.SelectedItem.ToString() == "Tanggal Mulai")
                    {
                        homeWindow.top.filter[2] = true;
                        homeWindow.top.filter[3] = false;
                        s = " WHERE (DATEDIFF(day, Tanggal_Mulai,'" + fromDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') <= 0) AND (DATEDIFF(day, Tanggal_Mulai,'" + toDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') >=0)";
                    }
                    else
                    {
                        homeWindow.top.filter[2] = false;
                        homeWindow.top.filter[3] = true;
                        s = " WHERE (DATEDIFF(day, Tanggal_Selesai,'" + fromDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') <= 0) AND (DATEDIFF(day, Tanggal_Selesai,'" + toDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') >=0)";
                    }
                }
                else if (homeWindow.top.tabel == topWindow.PEMBAYARAN)
                {
                    homeWindow.top.filter[4] = true;
                    s = " WHERE (DATEDIFF(day, Tanggal_Pembayaran,'" + fromDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') <= 0) AND (DATEDIFF(day, Tanggal_Pembayaran,'" + toDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') >=0)";
                }
                else if (homeWindow.top.tabel == topWindow.PEMBELIAN)
                {
                    if (attributFilterBox.SelectedItem.ToString() == "Nama Barang")
                    {
                        homeWindow.top.filter[5] = true;
                        homeWindow.top.filter[6] = false;
                        s = comboBox1.SelectedItem.ToString();
                    }
                    else
                    {
                        homeWindow.top.filter[5] = false;
                        homeWindow.top.filter[6] = true;
                        s = " WHERE (DATEDIFF(day, Tanggal,'" + fromDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') <= 0) AND (DATEDIFF(day, Tanggal,'" + toDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') >=0)";
                    }
                }
                else if (homeWindow.top.tabel == topWindow.PEMAKAIAN)
                {
                    homeWindow.top.filter[7] = true;
                    s = " WHERE (DATEDIFF(day, b.Tanggal,'" + fromDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') <= 0) AND (DATEDIFF(day, b.Tanggal,'" + toDatePicker.SelectedDate.Value.ToString("M/d/yyyy") + "') >=0)";
                }
            }
            homeWindow.top.refresh();
        }

        private void settingWindow_Cancel(object sender, CancelEventArgs e)
        {
            if (!homeWindow.top.close)
            {
                Visibility = System.Windows.Visibility.Hidden;
                e.Cancel = true;
            }
            else
            {
                Console.WriteLine("SETTING CLOSE");
            }
        }

        private void settingWindow_Closed(object sender, EventArgs e)
        {
            
        }

        private void settingWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            fromDatePicker.Visibility = System.Windows.Visibility.Hidden;
            FromLabel.Visibility = System.Windows.Visibility.Hidden;
            toDatePicker.Visibility = System.Windows.Visibility.Hidden;
            toLabel.Visibility = System.Windows.Visibility.Hidden;
            nameFilterBox.Visibility = System.Windows.Visibility.Visible;
            comboBox1.Visibility = System.Windows.Visibility.Hidden;
            isFirst = true;
            if (this.Visibility == System.Windows.Visibility.Visible)
            {
                if (homeWindow.top.tabel == topWindow.REKLAME)
                {
                    attributFilterBox.Items.Add("Kota");
                    attributFilterBox.Items.Add("Provinsi");
                }
                else if (homeWindow.top.tabel == topWindow.PENYEWAAN)
                {
                    attributFilterBox.Items.Add("Tanggal Mulai");
                    attributFilterBox.Items.Add("Tanggal Selesai");
                }
                else if (homeWindow.top.tabel == topWindow.PEMBAYARAN)
                {
                    attributFilterBox.Items.Add("Tanggal Pembayaran");
                }
                else if (homeWindow.top.tabel == topWindow.PEMBELIAN)
                {
                    attributFilterBox.Items.Add("Nama Barang");
                    attributFilterBox.Items.Add("Tanggal Pembelian");
                }
                else if (homeWindow.top.tabel == topWindow.PEMAKAIAN)
                {
                    attributFilterBox.Items.Add("Tanggal Pemakaian");
                }
            }
            else
            {
                attributFilterBox.Items.Clear();
            }
        }

        private void attributFilterBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (homeWindow.top.tabel == topWindow.PENYEWAAN)
            {
                nameFilterBox.Visibility = System.Windows.Visibility.Hidden;
                fromDatePicker.Visibility = System.Windows.Visibility.Visible;
                FromLabel.Visibility = System.Windows.Visibility.Visible;
                toLabel.Visibility = System.Windows.Visibility.Visible;
                toDatePicker.Visibility = System.Windows.Visibility.Visible;
                comboBox1.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (homeWindow.top.tabel == topWindow.PEMBAYARAN)
            {
                nameFilterBox.Visibility = System.Windows.Visibility.Hidden;
                fromDatePicker.Visibility = System.Windows.Visibility.Visible;
                FromLabel.Visibility = System.Windows.Visibility.Visible;
                toLabel.Visibility = System.Windows.Visibility.Visible;
                toDatePicker.Visibility = System.Windows.Visibility.Visible;
                comboBox1.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (homeWindow.top.tabel == topWindow.PEMBELIAN)
            {
                if (this.Visibility != System.Windows.Visibility.Hidden)
                {
                    if (attributFilterBox.SelectedItem.ToString() == "Tanggal Pembelian")
                    {
                        nameFilterBox.Visibility = System.Windows.Visibility.Hidden;
                        fromDatePicker.Visibility = System.Windows.Visibility.Visible;
                        FromLabel.Visibility = System.Windows.Visibility.Visible;
                        toLabel.Visibility = System.Windows.Visibility.Visible;
                        toDatePicker.Visibility = System.Windows.Visibility.Visible;
                        comboBox1.Visibility = System.Windows.Visibility.Hidden;
                    }
                    else
                    {
                        nameFilterBox.Visibility = System.Windows.Visibility.Hidden;
                        fromDatePicker.Visibility = System.Windows.Visibility.Hidden;
                        FromLabel.Visibility = System.Windows.Visibility.Hidden;
                        toLabel.Visibility = System.Windows.Visibility.Hidden;
                        toDatePicker.Visibility = System.Windows.Visibility.Hidden;
                        comboBox1.Visibility = System.Windows.Visibility.Visible;
                        if (isFirst)
                        {
                            try
                            {
                                homeWindow.Con.Open();
                                SqlDataAdapter da = new SqlDataAdapter("select nama_barang from daftar_barang order by ID_Barang", homeWindow.Con);
                                DataSet ds = new DataSet();
                                da.Fill(ds, "Daftar Barang");
                                homeWindow.Con.Close();
                                for (int i = 0; i < ds.Tables["Daftar Barang"].Rows.Count; i++)
                                {
                                    comboBox1.Items.Add(ds.Tables["Daftar Barang"].Rows[i].ItemArray.ElementAt(0).ToString());
                                }
                                isFirst = false;
                            }
                            catch (SqlException sqle)
                            {
                                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
                            }
                            
                        }
                    }
                }
            }
            else if (homeWindow.top.tabel == topWindow.PEMAKAIAN)
            {
                nameFilterBox.Visibility = System.Windows.Visibility.Hidden;
                fromDatePicker.Visibility = System.Windows.Visibility.Visible;
                FromLabel.Visibility = System.Windows.Visibility.Visible;
                toLabel.Visibility = System.Windows.Visibility.Visible;
                toDatePicker.Visibility = System.Windows.Visibility.Visible;
                comboBox1.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cancelButton_Click_1(object sender, RoutedEventArgs e)
        {
            homeWindow.top.isFilter = false;
            homeWindow.top.refresh();
        }
    }
}
